package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private AttrValueMapper attrValueMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private LabelInfoMapper labInfoMapper;
    @Resource
    private PaymentTypeMapper paymentTypeMapper;
    @Resource
    private FeeValueMapper feeValueMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;
    @Resource
    private LeaseAgreementMapper leaseAgreementMapper;



    @Override
    public IPage<RoomItemVo> pageRoomItemVo(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
         return roomInfoMapper.pageRoomItemVo(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomInfo roomInfo = this.getById(id);
        if (roomInfo==null){
            return null;
        }
        //根据id查询所属公寓信息
        ApartmentItemVo apartmentItemVoById = apartmentInfoService.getApartmentItemVoById(roomInfo.getApartmentId());
        //根据id查询图片信息
        List<GraphVo> graphVoList= graphInfoMapper.selectByIdgraphInfo(ItemType.ROOM,id);
        //根据id查询属性列表信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoById(id);
        //根据id查询配套信息列表
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListById(id);
        //根据id查询标签信息列表
        List<LabelInfo> labelInfoList = labInfoMapper.selectLabelInfoById(id);
        //根据id查询支付方式列表
        List<PaymentType> payTypeListByIdList = paymentTypeMapper.getPayTypeListById(id);
        //根据id查询杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVoById(roomInfo.getApartmentId());
        //根据id查询租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermById(id);

        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        queryWrapper.in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);
        Long count = leaseAgreementMapper.selectCount(queryWrapper);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted()==1);
        roomDetailVo.setIsCheckIn(count>0);

        roomDetailVo.setApartmentItemVo(apartmentItemVoById);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(payTypeListByIdList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageApartmentIdByRoomList(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageApartmentIdByRoomList(page, id);
    }
}




